clear all
set more off
cap log close

ssc install unique
    
global dir "/Replication Archive"
cd "${dir}"

global outreg_settings = " label bdec(4) pvalue pdec(3) tex(frag) excel  nor2 nonotes "


************************************
************* TABLE A8 *************
************************************

use DirectorID cycle icpsr2 chamber relevant amount_noself majority using "${dir}/Data/Final Datasets/Final_Data_Director_MC_Level_DEF2.dta", clear

gen donated=(amount_noself>0)
replace donated=donated*1000

collapse (max) relevant donated (first) majority, by(DirectorID cycle icpsr2 chamber)

sort DirectorID icpsr2 chamber cycle
by DirectorID icpsr2 chamber: gen event_0=(relevant==1 & relevant[_n-1]==0)

save "temp_entry.dta", replace

set more off

forvalues cycle=2002(2)2018 {

use "temp_entry.dta", clear

keep if cycle>=`cycle'-2 & cycle<=`cycle'

gen t=(cycle==`cycle' & event_0==1)
bys DirectorID icpsr2 chamber: egen treated=max(t)
gen control_t=(relevant==0 & treated==0)
drop t 
keep if control_t==1 | treated==1
bys DirectorID icpsr2 chamber: gen N=_N
keep if N==2
drop N
keep DirectorID cycle icpsr2 chamber relevant donated treated majority
gen period=.
replace period=1 if cycle==`cycle'-2
replace period=2 if cycle==`cycle'
gen treated_n=(treated==1 & period==2)
drop treated
rename treated_n treated
gen event=`cycle'
save "temp_event`cycle'_entry_c2.dta", replace

}

use "temp_event2002_entry_c2.dta", clear
append using "temp_event2004_entry_c2.dta"
append using "temp_event2006_entry_c2.dta"
append using "temp_event2008_entry_c2.dta"
append using "temp_event2010_entry_c2.dta"
append using "temp_event2012_entry_c2.dta"
append using "temp_event2014_entry_c2.dta"
append using "temp_event2016_entry_c2.dta"
append using "temp_event2018_entry_c2.dta"

egen double Director_MC_event=group(DirectorID icpsr2 chamber event)
egen double Director_MC=group(DirectorID icpsr2 chamber)
egen double Period_MC_event=group(period icpsr2 chamber event)
egen double Period_Director_event=group(DirectorID period chamber event)

unique DirectorID 
local unique_directors: display %5.4fc `r(unique)' 
unique icpsr2 
local unique_MC: display %5.4fc `r(unique)' 

bys icpsr2 chamber event: egen min_majority=min(majority)
bys icpsr2 chamber event: egen max_majority=max(majority)

reghdfe donated treated if min_majority==1 & max_majority==1, absorb(Director_MC_event Period_MC_event) cluster(Director_MC) compact keepsingletons
su donated if relevant ==0 & period==1 & e(sample)==1, d
local mean: display %5.4fc `r(mean)' 
local effect = _b[treated]/`mean'*100
local effect: display %5.4fc `effect'
outreg2 using "${dir}/Results/TableA8_PanelA", $outreg_settings addtext(Mean, `mean', Increase, `effect', Num. Individuals, `unique_directors', Num. Companies, `unique_company', Num. MCs, `unique_MC', Individual-MC FE, Yes, MC-Cycle FE, Yes, Individual-Cycle FE, Yes)  nocons

reghdfe donated treated if min_majority==1 & max_majority==1 & chamber=="H", absorb(Director_MC_event Period_MC_event) cluster(Director_MC) compact keepsingletons
su donated if relevant ==0 & period==1 & e(sample)==1, d
local mean: display %5.4fc `r(mean)' 
local effect = _b[treated]/`mean'*100
local effect: display %5.4fc `effect'
outreg2 using "${dir}/Results/TableA8_PanelA", $outreg_settings addtext(Mean, `mean', Increase, `effect', Num. Individuals, `unique_directors', Num. Companies, `unique_company', Num. MCs, `unique_MC', Individual-MC FE, Yes, MC-Cycle FE, Yes, Individual-Cycle FE, Yes)  nocons

reghdfe donated treated if min_majority==1 & max_majority==1 & chamber=="S", absorb(Director_MC_event Period_MC_event) cluster(Director_MC) compact keepsingletons
su donated if relevant ==0 & period==1 & e(sample)==1, d
local mean: display %5.4fc `r(mean)' 
local effect = _b[treated]/`mean'*100
local effect: display %5.4fc `effect'
outreg2 using "${dir}/Results/TableA8_PanelA", $outreg_settings addtext(Mean, `mean', Increase, `effect', Num. Individuals, `unique_directors', Num. Companies, `unique_company', Num. MCs, `unique_MC', Individual-MC FE, Yes, MC-Cycle FE, Yes, Individual-Cycle FE, Yes)  nocons

reghdfe donated treated if min_majority==0 & max_majority==0, absorb(Director_MC_event Period_MC_event) cluster(Director_MC) compact keepsingletons
su donated if relevant ==0 & period==1 & e(sample)==1, d
local mean: display %5.4fc `r(mean)' 
local effect = _b[treated]/`mean'*100
local effect: display %5.4fc `effect'
outreg2 using "${dir}/Results/TableA8_PanelA", $outreg_settings addtext(Mean, `mean', Increase, `effect', Num. Individuals, `unique_directors', Num. Companies, `unique_company', Num. MCs, `unique_MC', Individual-MC FE, Yes, MC-Cycle FE, Yes, Individual-Cycle FE, Yes)  nocons

reghdfe donated treated if min_majority==0 & max_majority==0 & chamber=="H", absorb(Director_MC_event Period_MC_event) cluster(Director_MC) compact keepsingletons
su donated if relevant ==0 & period==1 & e(sample)==1, d
local mean: display %5.4fc `r(mean)' 
local effect = _b[treated]/`mean'*100
local effect: display %5.4fc `effect'
outreg2 using "${dir}/Results/TableA8_PanelA", $outreg_settings addtext(Mean, `mean', Increase, `effect', Num. Individuals, `unique_directors', Num. Companies, `unique_company', Num. MCs, `unique_MC', Individual-MC FE, Yes, MC-Cycle FE, Yes, Individual-Cycle FE, Yes)  nocons

reghdfe donated treated if min_majority==0 & max_majority==0 & chamber=="S", absorb(Director_MC_event Period_MC_event) cluster(Director_MC) compact keepsingletons
su donated if relevant ==0 & period==1 & e(sample)==1, d
local mean: display %5.4fc `r(mean)' 
local effect = _b[treated]/`mean'*100
local effect: display %5.4fc `effect'
outreg2 using "${dir}/Results/TableA8_PanelA", $outreg_settings addtext(Mean, `mean', Increase, `effect', Num. Individuals, `unique_directors', Num. Companies, `unique_company', Num. MCs, `unique_MC', Individual-MC FE, Yes, MC-Cycle FE, Yes, Individual-Cycle FE, Yes)  nocons

erase "temp_entry.dta"
forvalues i=2002(2)2018 {
erase "temp_event`i'_entry_c2.dta"
}

use DirectorID cycle icpsr2 chamber relevant amount_noself majority using "${dir}/Data/Final Datasets/Final_Data_Director_MC_Level_DEF2.dta", clear

gen donated=(amount_noself>0)
replace donated=donated*1000

collapse (max) relevant donated (first) majority, by(DirectorID cycle icpsr2 chamber)

sort DirectorID icpsr2 chamber cycle
by DirectorID icpsr2 chamber: gen event_0=(relevant==0 & relevant[_n-1]==1)

save "temp_exit.dta", replace

set more off

forvalues cycle=2002(2)2018 {

use "temp_exit.dta", clear

keep if cycle>=`cycle'-2 & cycle<=`cycle'

gen t=(cycle==`cycle' & event_0==1)
bys DirectorID icpsr2 chamber: egen treated=max(t)
gen control_t=(relevant==0 & treated==0)
drop t 
keep if control_t==1 | treated==1
bys DirectorID icpsr2 chamber: gen N=_N
keep if N==2
drop N
keep DirectorID cycle icpsr2 chamber relevant donated treated majority
gen period=.
replace period=1 if cycle==`cycle'-2
replace period=2 if cycle==`cycle'
gen treated_n=(treated==1 & period==2)
drop treated
rename treated_n treated
gen event=`cycle'
save "temp_event`cycle'_exit_c2.dta", replace

}

use "temp_event2002_exit_c2.dta", clear
append using "temp_event2004_exit_c2.dta"
append using "temp_event2006_exit_c2.dta"
append using "temp_event2008_exit_c2.dta"
append using "temp_event2010_exit_c2.dta"
append using "temp_event2012_exit_c2.dta"
append using "temp_event2014_exit_c2.dta"
append using "temp_event2016_exit_c2.dta"
append using "temp_event2018_exit_c2.dta"

egen double Director_MC_event=group(DirectorID icpsr2 chamber event)
egen double Director_MC=group(DirectorID icpsr2 chamber)
egen double Period_MC_event=group(period icpsr2 chamber event)
egen double Period_Director_event=group(DirectorID period chamber event)

unique DirectorID 
local unique_directors: display %5.4fc `r(unique)' 
unique icpsr2 
local unique_MC: display %5.4fc `r(unique)' 

bys icpsr2 chamber event: egen min_majority=min(majority)
bys icpsr2 chamber event: egen max_majority=max(majority)

reghdfe donated treated if min_majority==1 & max_majority==1, absorb(Director_MC_event Period_MC_event) cluster(Director_MC) compact keepsingletons
su donated if relevant ==1 & period==1 & e(sample)==1, d
local mean: display %5.4fc `r(mean)' 
local effect = _b[treated]/`mean'*100
local effect: display %5.4fc `effect'
outreg2 using "${dir}/Results/TableA8_PanelB", $outreg_settings addtext(Mean, `mean', Increase, `effect', Num. Individuals, `unique_directors', Num. Companies, `unique_company', Num. MCs, `unique_MC', Individual-MC FE, Yes, MC-Cycle FE, Yes, Individual-Cycle FE, Yes)  nocons

reghdfe donated treated if min_majority==1 & max_majority==1 & chamber=="H", absorb(Director_MC_event Period_MC_event) cluster(Director_MC) compact keepsingletons
su donated if relevant ==1 & period==1 & e(sample)==1, d
local mean: display %5.4fc `r(mean)' 
local effect = _b[treated]/`mean'*100
local effect: display %5.4fc `effect'
outreg2 using "${dir}/Results/TableA8_PanelB", $outreg_settings addtext(Mean, `mean', Increase, `effect', Num. Individuals, `unique_directors', Num. Companies, `unique_company', Num. MCs, `unique_MC', Individual-MC FE, Yes, MC-Cycle FE, Yes, Individual-Cycle FE, Yes)  nocons

reghdfe donated treated if min_majority==1 & max_majority==1 & chamber=="S", absorb(Director_MC_event Period_MC_event) cluster(Director_MC) compact keepsingletons
su donated if relevant ==1 & period==1 & e(sample)==1, d
local mean: display %5.4fc `r(mean)' 
local effect = _b[treated]/`mean'*100
local effect: display %5.4fc `effect'
outreg2 using "${dir}/Results/TableA8_PanelB", $outreg_settings addtext(Mean, `mean', Increase, `effect', Num. Individuals, `unique_directors', Num. Companies, `unique_company', Num. MCs, `unique_MC', Individual-MC FE, Yes, MC-Cycle FE, Yes, Individual-Cycle FE, Yes)  nocons

reghdfe donated treated if min_majority==0 & max_majority==0, absorb(Director_MC_event Period_MC_event) cluster(Director_MC) compact keepsingletons
su donated if relevant ==1 & period==1 & e(sample)==1, d
local mean: display %5.4fc `r(mean)' 
local effect = _b[treated]/`mean'*100
local effect: display %5.4fc `effect'
outreg2 using "${dir}/Results/TableA8_PanelB", $outreg_settings addtext(Mean, `mean', Increase, `effect', Num. Individuals, `unique_directors', Num. Companies, `unique_company', Num. MCs, `unique_MC', Individual-MC FE, Yes, MC-Cycle FE, Yes, Individual-Cycle FE, Yes)  nocons

reghdfe donated treated if min_majority==0 & max_majority==0 & chamber=="H", absorb(Director_MC_event Period_MC_event) cluster(Director_MC) compact keepsingletons
su donated if relevant ==1 & period==1 & e(sample)==1, d
local mean: display %5.4fc `r(mean)' 
local effect = _b[treated]/`mean'*100
local effect: display %5.4fc `effect'
outreg2 using "${dir}/Results/TableA8_PanelB", $outreg_settings addtext(Mean, `mean', Increase, `effect', Num. Individuals, `unique_directors', Num. Companies, `unique_company', Num. MCs, `unique_MC', Individual-MC FE, Yes, MC-Cycle FE, Yes, Individual-Cycle FE, Yes)  nocons

reghdfe donated treated if min_majority==0 & max_majority==0 & chamber=="S", absorb(Director_MC_event Period_MC_event) cluster(Director_MC) compact keepsingletons
su donated if relevant ==1 & period==1 & e(sample)==1, d
local mean: display %5.4fc `r(mean)' 
local effect = _b[treated]/`mean'*100
local effect: display %5.4fc `effect'
outreg2 using "${dir}/Results/TableA8_PanelB", $outreg_settings addtext(Mean, `mean', Increase, `effect', Num. Individuals, `unique_directors', Num. Companies, `unique_company', Num. MCs, `unique_MC', Individual-MC FE, Yes, MC-Cycle FE, Yes, Individual-Cycle FE, Yes)  nocons

erase "temp_exit.dta"
forvalues i=2002(2)2018 {
erase "temp_event`i'_exit_c2.dta"
}
